<template>
  <div>
    <el-table
      :data="list"
      style="width: 100%;margin-bottom: 20px;"
      row-key="id"
      border
      :tree-props="{children: 'children'}"
    >
      <el-table-column prop="id" label="商品编号"></el-table-column>
      <el-table-column prop="goodsname" label="商品名称"></el-table-column>
      <el-table-column label="商品价格">
        <template slot-scope="scope">{{scope.row.price}}</template>
      </el-table-column>
      <el-table-column label="市场价格">
        <template slot-scope="scope">{{scope.row.market_price}}</template>
      </el-table-column>
      <el-table-column label="图片">
        <template slot-scope="scope">
          <img :src="$pre+scope.row.img" alt />
        </template>
      </el-table-column>
      <el-table-column label="是否新品">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.isnew==1">是</el-button>
          <el-button type="danger" v-else>否</el-button>
        </template>
      </el-table-column>

      <el-table-column label="是否热卖">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.ishot==1">是</el-button>
          <el-button type="danger" v-else>否</el-button>
        </template>
      </el-table-column>

      <el-table-column label="状态">
        <template slot-scope="scope">
          <el-button type="primary" v-if="scope.row.status==1">启用</el-button>
          <el-button type="info" v-else>禁用</el-button>
        </template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" @click="willEdit(scope.row.id)">编辑</el-button>
          <del-btn @confirm="del(scope.row.id)"></del-btn>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <!-- 
        total：总数
        layout="prev, pager, next" prev表示上一页，next为下一页，pager表示页码列表
        page-size:设置一页的数量
    -->
    <el-pagination
      layout="prev, pager, next"
      :total="count"
      :page-size="size"
      background
      @current-change="changePage"
    ></el-pagination>
  </div>
</template>
<script>
import { reqgoodsdelete } from "../../../request/api";
import { successAlert } from "../../../utils/alert";
import { mapGetters, mapActions } from "vuex";
export default {
  components: {},

  computed: {
    ...mapGetters({
      list: "goods/list",
      count: "goods/count",
      size: "goods/size",
    }),
  },
  mounted() {
    this.reqList();
    this.reqCount();
  },
  methods: {
    ...mapActions({
      //请list
      reqList: "goods/reqList",
      //请总数
      reqCount: "goods/reqCount",
      //修改页码
      changePage: "goods/changePage",
    }),
    //5.点击了编辑
    willEdit(id) {
      this.$emit("edit", id);
    },
    //删除
    del(id) {
      //点了确定
      reqgoodsdelete({ id: id }).then((res) => {
        if (res.data.code == 200) {
          //弹成功
          successAlert(res.data.msg);
          //刷list
          this.reqList();
          this.reqCount();
        }
      });
    },
    sendFile(file) {
      this.form.img = file;
    },
  },
};
</script>
<style lang="less" scoped>
img {
  width: 100px;
}
</style>